package net.atomarrow.render;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import net.atomarrow.ioc.ActionContext;
import net.atomarrow.util.CaptchaCodeUtil;

public class CaptchaRender extends Render {
	/**
	 * 验证码key
	 */
	public static final String SESSION_CAPTCHACODE="SESSION_CAPTCHACODE";
	public static final String WIDTH="WIDTH";
	public static final String HEIGHT="HEIGHT";
	public static final String COUNT="COUNT";
    @Override
    public void render() {
    	ActionContext actionContext=ActionContext.getContext();
    	 HttpServletResponse response = actionContext.getResponse();
         response.reset();
         Integer width=getAttr(WIDTH);
         Integer height=getAttr(HEIGHT);
         Integer count=getAttr(COUNT);
         if(width==null){width=130;}
         if(height==null){height=40;}
         if(count==null){count=4;}
    	CaptchaCodeUtil captchaCodeUtil=new CaptchaCodeUtil(width,height,count);
    	  // 设置响应的类型格式为图片格式
        response.setContentType("image/jpeg");
        //禁止图像缓存。
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        ActionContext.getContext().getSession().setAttribute(SESSION_CAPTCHACODE, captchaCodeUtil.getCode());
        try {
			captchaCodeUtil.write(response.getOutputStream());
		} catch (IOException e) {
			e.printStackTrace();
		}
    }

}
